home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / VARREC.PAS < prev    next >
Pascal/Delphi Source File  |  1989-06-30  |  2KB  |  62 lines

  1. PROGRAM variant_record_example;
  2.  
  3. TYPE kind_of_vehicle = (car,truck,bicycle,boat);
  4.  
  5.      vehicle = RECORD
  6.        owner_name   : STRING[25];
  7.        gross_weight : INTEGER;
  8.        value        : REAL;
  9.        CASE what_kind : kind_of_vehicle OF
  10.          car     : (wheels : INTEGER;
  11.                     engine : STRING[8]);
  12.          truck   : (motor  : STRING[8];
  13.                     tires  : INTEGER;
  14.                     payload : INTEGER);
  15.          bicycle : (tyres   : INTEGER);
  16.          boat    : (prop_blades : BYTE;
  17.                     sail    : BOOLEAN;
  18.                     power   : STRING[8]);
  19.        END; (* of RECORD *)
  20.  
  21. VAR sunfish,ford,schwinn,mac : vehicle;
  22.  
  23. BEGIN  (* main program *)
  24.  
  25.   ford.owner_name := 'Walter'; (* fields defined in order *)
  26.   ford.gross_weight := 5750;
  27.   ford.value := 2595.00;
  28.   ford.what_kind := truck;
  29.   ford.motor := 'V8';
  30.   ford.tires := 18;
  31.   ford.payload := 12000;
  32.  
  33.   WITH sunfish DO
  34.   BEGIN
  35.     what_kind := boat; (* fields defined in random order *)
  36.     sail := TRUE;
  37.     prop_blades := 3;
  38.     power := 'wind';
  39.     gross_weight := 375;
  40.     value := 1300.00;
  41.     owner_name := 'Herman and George';
  42.   END;
  43.  
  44.   ford.engine := 'flathead';  (* tag-field not defined yet but it *)
  45.   ford.what_kind := car;      (* must be before it can be used    *)
  46.   ford.wheels := 4;
  47.     (* notice that the non variant part is not redefined here *)
  48.  
  49.   mac := sunfish; (* entire record copied, including the tag-field *)
  50.  
  51.   IF ford.what_kind = car THEN        (* this should print *)
  52.     WRITELN(ford.owner_name,' owns the car with a ',ford.engine,
  53.             ' engine');
  54.  
  55.   IF sunfish.what_kind = bicycle THEN  (* this should not print *)
  56.     WRITELN('The sunfish is a bicycle which it shouldn''t be');
  57.  
  58.   IF mac.what_kind = boat THEN         (* this should print *)
  59.     WRITELN('The mac is now a boat with',mac.prop_blades:2,
  60.              ' propeller blades.');
  61.  
  62. END.  (* of main program *)